package com.cjjd.c.base;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.cjjd.c.R;
import com.cjjd.c.http.RetrofitInterface;
import com.cjjd.c.http.RetrofitSingleton;
import com.cjjd.c.view.ActionBarView;

import butterknife.ButterKnife;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/9.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public abstract class ActionBarActivity extends FragmentActivity implements ActionBarView.ActionBarListener {


    /**
     * 全局activity
     */
    protected FragmentActivity mContext;
    protected FragmentManager mFragmentManager;
    protected RetrofitInterface service;
    protected ActionBarView mActionBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
        service = RetrofitSingleton.getService(this);

        mFragmentManager = getSupportFragmentManager();

        setContentView(R.layout.activity_actionbar_base);


        if (getContentView() != 0) {
            ViewGroup root = (ViewGroup) findViewById(R.id.activity_root);
            CoordinatorLayout frameLayout = (CoordinatorLayout) root.findViewById(R.id.activity_body);


            View.inflate(mContext, getContentView(), frameLayout);
            //frameLayout.addView(contentView);


            mActionBar = (ActionBarView) root.findViewById(R.id.actionbar_title);
            mActionBar.setTitle(getTitleString());
            //左键监听
            mActionBar.getLeftButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLeftButtonClick();
                }
            });
            //右键监听
            mActionBar.getRightButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightButtonClick();
                }
            });

            initLeftButton(mActionBar.getLeftButton());
            initRightButton(mActionBar.getRightButton());

        } else {

        }
//        if (savedInstanceState == null && getContentFragment() != null) {
//            add(getContentFragment());
//        }

        if (Build.VERSION.SDK_INT >= 21) {
//            getWindow().setStatusBarColor(getResources().getColor(R.color.black));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        try {
            super.onPause();
        } catch (Exception e) {
        }
        ;
    }

    @Override
    protected void onStop() {
        super.onStop();


    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
        } catch (Exception e) {
        }

    }

    //public abstract Fragment getContentFragment();

    public abstract int getContentView();

    public void remove(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .remove(fragment)
                .commit();
    }

    public void show(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .show(fragment)
                .commit();
    }

    public void hide(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(fragment)
                .commit();

    }

    public void add(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }

    public void add(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment, tag)
                .commit();
    }

    public void add(int resId, Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .add(resId, fragment, tag)
                .commit();
    }

    public void replace(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment, tag)
                .commit();
    }


    //<editor-fold desc="ActionBar Init">
    @Override
    public void onLeftButtonClick() {
        mContext.onBackPressed();
    }

    @Override
    public void onRightButtonClick() {

    }

    @Override
    public void initLeftButton(Button leftButton) {

    }

    @Override
    public void initRightButton(Button rightButton) {

    }

    public void setTitle(Object obj) {
        mActionBar.setTitle(obj);
    }
    //</editor-fold>


}
